package com.ruoyi.generator.component;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.generator.mapper.AppMapper;
import com.ruoyi.generator.vo.MaintenanceAppVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@EnableScheduling
public class MaintenanceTask {
    @Autowired
    private AppMapper appMapper;

    /**
     * 每日零点定时任务（开机自动执行一次PostConstruct）
     */
//    @Scheduled(cron = "0 0 0 * * ?")
//    @Scheduled(fixedDelay = 5000)
    public void circumferenceTask() {
        MaintenanceAppVO maintenanceApp = new MaintenanceAppVO();
        List<MaintenanceAppVO> maintenanceAppVOS = appMapper.getmaintenanceStatus("0");
        System.out.println(maintenanceAppVOS);
        for (MaintenanceAppVO maintenanceAppVO : maintenanceAppVOS) {
            //获取保养开始时间
            Date startDate = maintenanceAppVO.getStartDate();
            //转换成LocalDate类型
            LocalDate localDate = startDate.toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate();
            LocalDate today = LocalDate.now();
            if (maintenanceAppVO.getMaintenancePeriod() == 2) {
                //每周
                String time = maintenanceAppVO.getTime();
                String week = StringUtils.substringAfter(time, "到");
                if (week.equals("周日")) {
                    continue;
                } else {
                    //获取每周的班次时间，在基础上加1天
                    Integer maintenanceOfWeek = chineseToDay.get(week);
                    int newWeek = maintenanceOfWeek + 1;
                    //当前是周几
                    int dayOfWeek = today.getDayOfWeek().getValue();
                    if (dayOfWeek >= newWeek){
                        maintenanceApp.setMaintenanceId(maintenanceAppVO.getMaintenanceId());
                        maintenanceApp.setMaintenanceStatus("2");
                        appMapper.updateMaintenanceStatus(maintenanceApp);
                    }
                }
            } else if (maintenanceAppVO.getMaintenancePeriod() == 3) {
                //每月
                //根据保养开始时间往后延30天
                LocalDate localDateNew = localDate.plusDays(30);
//                LocalDate today = LocalDate.now();
                if (today.isAfter(localDateNew)) {
                    maintenanceApp.setMaintenanceId(maintenanceAppVO.getMaintenanceId());
                    maintenanceApp.setMaintenanceStatus("2");
                    appMapper.updateMaintenanceStatus(maintenanceApp);
                }
            } else if (maintenanceAppVO.getMaintenancePeriod() == 4) {
                //每季度
                //根据保养开始时间往后延90天
                LocalDate localDateNew = localDate.plusDays(90);
//                LocalDate today = LocalDate.now();
                if (today.isAfter(localDateNew)) {
                    maintenanceApp.setMaintenanceId(maintenanceAppVO.getMaintenanceId());
                    maintenanceApp.setMaintenanceStatus("2");
                    appMapper.updateMaintenanceStatus(maintenanceApp);
                }
            } else if (maintenanceAppVO.getMaintenancePeriod() == 5) {
                //每年
                //根据保养开始时间往后延365天
                LocalDate localDateNew = localDate.plusDays(365);
//                LocalDate today = LocalDate.now();
                if (today.isAfter(localDateNew)) {
                    maintenanceApp.setMaintenanceId(maintenanceAppVO.getMaintenanceId());
                    maintenanceApp.setMaintenanceStatus("2");
                    appMapper.updateMaintenanceStatus(maintenanceApp);
                }
            }
        }
    }

    private static final Map<String, Integer> chineseToDay = new HashMap<>();

    static {
        chineseToDay.put("周一", 1);
        chineseToDay.put("周二", 2);
        chineseToDay.put("周三", 3);
        chineseToDay.put("周四", 4);
        chineseToDay.put("周五", 5);
        chineseToDay.put("周六", 6);
        chineseToDay.put("周日", 7);
    }
}
